﻿Imports System.IO
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
'system 系统 Net 处理网络
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text

Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Reflection.Emit
Public Class chat
    Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer
    Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
    Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer
    Private Declare Function BitBlt Lib "GDI32" (ByVal srchDC As Integer, ByVal srcX As Integer, ByVal srcY As Integer, ByVal srcW As Integer, ByVal srcH As Integer, ByVal desthDC As Integer, ByVal destX As Integer, ByVal destY As Integer, ByVal op As Integer) As Integer
    Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Integer) As Integer
    Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer
    Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer
    Const SRCCOPY As Integer = &HCC0020
    Private Sub jietuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles jietuToolStripMenuItem.Click
        Dim hDC, hMDC As Integer
        Dim hBMP, hBMPOld As Integer
        Dim sw, sh As Integer

        hDC = GetDC(0)
        hMDC = CreateCompatibleDC(hDC)
        sw = Screen.PrimaryScreen.Bounds.Width
        sh = Screen.PrimaryScreen.Bounds.Height
        hBMP = CreateCompatibleBitmap(hDC, sw, sh)
        hBMPOld = SelectObject(hMDC, hBMP)
        BitBlt(hMDC, 0, 0, sw, sh, hDC, 0, 0, SRCCOPY)
        hBMP = SelectObject(hMDC, hBMPOld)

        PictureBox1.Image = Image.FromHbitmap(New IntPtr(hBMP))        '保存图片
        DeleteDC(hDC)
        DeleteDC(hMDC)
        DeleteObject(hBMP)
        Me.jietuToolStripMenuItem.Enabled = True

    End Sub

    '保存图像-在chat中添加一个button
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        '在form中增加一个OpenFileDialog1控件
        Dim ofd As OpenFileDialog = Me.OpenFileDialog1
        ofd.Filter = "jpg file|*.jpg|bmp file|*.bmp"
        Dim bmp As Bitmap = Me.PictureBox1.Image
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            bmp.Save(ofd.FileName)
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        RichTextBox3.Text = ""
    End Sub

    Private Sub noneToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles noneToolStripMenuItem.Click
        RichTextBox1.Text = ""
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        '获取第二个文本的内容，就是text
        Dim chatContent = ""
        Dim currentTime As Date
        currentTime = DateTime.Now
        chatContent = RichTextBox2.Text
        RichTextBox1.Text = RichTextBox3.Text + vbCrLf + currentTime + vbCrLf + chatContent
        RichTextBox3.Text = ""
        RichTextBox2.Text = RichTextBox1.Text + vbCrLf + currentTime + vbCrLf + chatContent
    End Sub

    Private Sub fontToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles fontToolStripMenuItem.Click
        Dim dlg As New Windows.Forms.FontDialog
        If dlg.ShowDialog() = DialogResult.OK Then
            Console.WriteLine(dlg.Font.ToString())
            RichTextBox1.Font = dlg.Font                        '改变字体
            RichTextBox2.Font = dlg.Font
            RichTextBox3.Font = dlg.Font
        End If
    End Sub

    Private Sub fontcolorToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles fontcolorToolStripMenuItem.Click
        Dim dlg2 As New Windows.Forms.ColorDialog
        If dlg2.ShowDialog() = DialogResult.OK Then
            Console.WriteLine(dlg2.Color.ToString())
            RichTextBox1.ForeColor = dlg2.Color                '改变字体颜色
            RichTextBox2.ForeColor = dlg2.Color
            RichTextBox3.ForeColor = dlg2.Color
        End If
    End Sub

    Private Sub savehistoryToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles savehistoryToolStripMenuItem.Click
        ' 获取RichTextBox中的文本
        Dim richTextBoxContent As String = RichTextBox1.Text

        ' 选择要保存的文件路径和文件名（这里假设用户选择保存）
        Dim saveFileDialog As New SaveFileDialog()
        saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        If saveFileDialog.ShowDialog() = DialogResult.OK Then
            ' 将内容写入选定的文件
            Using writer As New StreamWriter(saveFileDialog.FileName)
                writer.WriteLine(richTextBoxContent)
            End Using

            MessageBox.Show("您的聊天历史内容已成功保存到：" & saveFileDialog.FileName)
        End If
    End Sub

    Private Sub qingkongToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles qingkongToolStripMenuItem.Click
        RichTextBox2.Text = ""         '清空
    End Sub
End Class